package com.itheima.health.service.impl;

import com.itheima.health.common.Globalconstant;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.MobileCommonDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MobileService;
import com.itheima.health.vo.MobileSubmitSetMealVO;
import com.itheima.health.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class MobileServiceImpl implements MobileService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    MemberDao memberDao;
    @Autowired
    OrderSettingDao orderSettingDao;
    @Autowired
    SetMealDao setMealDao;
    @Autowired
    private MobileCommonDao mobileCommonDao;

    @Override
    public void sendCode(String type, String telephone){
        //生成随机数
        int code = new Random().nextInt(10000) + 1000;
        log.info("code={}", code);
        //发送随机数
        //......
        //储存redis
        ValueOperations operations = redisTemplate.opsForValue();
        redisTemplate.delete("code:"+telephone);
        operations.set("code:"+telephone,code,3*60, TimeUnit.MINUTES);


    }
    /*
         用户验证,是否是老用户,新用户自动注册
     */
    @Override
    public void login(MobileSubmitSetMealVO mobileSubmitSetMealVO) {

        //验证验证码
        if (StringUtils.isBlank(mobileSubmitSetMealVO.getTelephone())) {
            throw new BusinessRuntimeException("请传入正确的手机号");
        }
        String inCode = redisTemplate.opsForValue().get("code:" + mobileSubmitSetMealVO.getTelephone())+"";
        String frontCode = mobileSubmitSetMealVO.getValidateCode();
        if (StringUtils.isBlank(inCode)) {
            throw new BusinessRuntimeException("请发送验证");
        }
        if (StringUtils.isBlank(frontCode)){
            throw new BusinessRuntimeException("验证码不能为空");
        }
        if (!inCode.equals(frontCode)) {
            throw new BusinessRuntimeException("验证码错误");
        }
        redisTemplate.delete("code:" + mobileSubmitSetMealVO.getTelephone());

        //是否是老用户

        Integer id = memberDao.selectMemIdByPhoneNumber(mobileSubmitSetMealVO.getTelephone());
        if (id != null) {
            //老用户
            mobileSubmitSetMealVO.setMemberId(id);
            return;
        }
        mobileSubmitSetMealVO.setRegTime(new Date());
        //新用户注册
        memberDao.add(mobileSubmitSetMealVO);
    }
/*
判断该体检是否约满,是否重复预约,
 */
    @Override
    public void verifySubmit(MobileSubmitSetMealVO mobileSubmitSetMealVO) {
        //判断该体检是否约满
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Globalconstant.EXCEL_DATA_PATTERN);
        String orderDate = simpleDateFormat.format(mobileSubmitSetMealVO.getOrderDate());
        log.info("orderDate={}",orderDate);
        List<OrderSetting> orderSettings = orderSettingDao.getOrderSettingByMonth(orderDate, orderDate);
        if (orderSettings.isEmpty()) {
            throw new BusinessRuntimeException("一会再来吧,还没开放预约呢");
        }
        OrderSetting orderSetting = orderSettings.get(0);
        if (orderSetting.getNumber()==orderSetting.getReservations()){
            throw new BusinessRuntimeException("来晚了,预约人满了");
        }
        //是否重复预约
        Integer count = mobileCommonDao.getOrderByMemberIdAndData(mobileSubmitSetMealVO.getMemberId(),mobileSubmitSetMealVO.getOrderDate());
        if (count != 0) {
            throw new BusinessRuntimeException("预约过了,不可以重复预约哟");
        }
    }

    @Override
    @Transactional
    public OrderSetting submit(MobileSubmitSetMealVO mobileSubmitSetMealVO) {
        //t_oder
        mobileSubmitSetMealVO.setOrderType("微信预约");
        mobileSubmitSetMealVO.setOrderStatus("未到诊");
        mobileCommonDao.insetOrder(mobileSubmitSetMealVO);
        //t_oderSetting

        Date orderDate = mobileSubmitSetMealVO.getOrderDate();
        mobileCommonDao.addReservationsByorderDate(orderDate);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Globalconstant.EXCEL_DATA_PATTERN);
        String orderDate1 = simpleDateFormat.format(mobileSubmitSetMealVO.getOrderDate());
        log.info("orderDate={}",orderDate);
        OrderSetting orderSetting = orderSettingDao.getOrderSettingByMonth(orderDate1, orderDate1).get(0);
        orderSetting.setId(mobileSubmitSetMealVO.getId());
        return orderSetting;

    }

    @Override
    public OrderVO selectOrderInfoByOrderid(Integer id) {
        OrderVO orderVO = mobileCommonDao.getOrderByid(id);
        log.info("orderVO={}",orderVO);
        String memberName = memberDao.getMemberNameById(orderVO.getMember_id());
        orderVO.setMember(memberName);
        Setmeal setmeal = setMealDao.selectSetMealByid(orderVO.getSetmeal_id());
        orderVO.setSetmeal(setmeal.getName());
        return orderVO;
    }

}
